unit cdemo_rf;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls;

type
  Tfrm_demo_rf = class(TForm)
    List1: TListBox;
    Button1: TButton;
    Button2: TButton;
    Button4: TButton;
    Button5: TButton;
    Button3: TButton;
    CheckBox1: TCheckBox;
    ComboBox1: TComboBox;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button4Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure Button5Click(Sender: TObject);
    function convertChararrToHexstr(chararr:PAnsichar;cnt: Integer):string ;
    procedure FormCreate(Sender: TObject);
    procedure ComboBox1Change(Sender: TObject);

  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  frm_demo_rf:Tfrm_demo_rf;
  hexkey2:ansichar;
  tagtype :Longint;
  snr:longword;
  data32:ansichar;
  databuff32,datadevstr:ansichar;
  rvalue:Longint;
  wvalue:Longint;
  cardmode:Integer;
  loadmode:Integer;
  sector:Integer;
  address:Integer;
  size:Byte;
  tempint:longword;
  st:smallint;
  readdata:array[0..32]of ansichar;
  curCardType:Integer;

  hdevs:Array [0..9] of integer=(-1,-1,-1,-1,-1,-1,-1,-1,-1,-1);    //假设当前有不到10台
implementation

uses drv_unit;

{$R *.DFM}
function Tfrm_demo_rf.convertChararrToHexstr(chararr:PAnsichar;cnt: Integer):string;
var
 str:string;
 tempstr:string;
 I:integer;
begin

for I := 0 to cnt-1 do
begin
tempstr:=inttohex(integer(chararr[i]),2);
str:=str+tempstr;
end;

Result:=str;
end;

procedure Tfrm_demo_rf.Button1Click(Sender: TObject);
var
 tmpdev:integer;
 i:integer;

begin
if CheckBox1.Checked=true then begin

//如果当前连接有多台读卡器，连续调用fw_init可以获得每个设备的句柄
 i:=0;
 repeat
 tmpdev:=fw_init(100,0);

 if tmpdev>0 then begin
 hdevs[i]:=tmpdev;
 i:=i+1;
 end;

 until (tmpdev<0);

 icdev:=hdevs[0];

end

else begin
icdev:=fw_init(0,115200); //串口1，波特率设为115200
end;

  If icdev < 0 Then begin
      List1.items.Add('调用 fw_init()函数出错!');
      exit;
 end
   else  begin
   List1.Items.add('调用 fw_init()函数成功!');
   end;
  end;

procedure Tfrm_demo_rf.Button2Click(Sender: TObject);
var   keyarr : Array [0..6] of ansichar  ;
begin
 keyarr[0]:=ansichar($ff);
 keyarr[1]:=ansichar($ff);
 keyarr[2]:=ansichar($ff);
 keyarr[3]:=ansichar($ff);
 keyarr[4]:=ansichar($ff);
 keyarr[5]:=ansichar($ff);

 //hexkey := 'a0a1a2a3a4a5';
 st := fw_load_key(icdev, 0, 1,@keyarr);
 If st <> 0 Then begin
      List1.items.add('调用 fw_Load_key()函数出错!');
      exit;
 end;

 List1.items.add('调用 fw_Load_key()函数成功!');

 end;

procedure Tfrm_demo_rf.Button3Click(Sender: TObject);
 var
 sendbuf: Array[0..16] of ansichar;
 str:string;
 value:LongWord;
 cardbuf:Array[0..8] of ansichar; // 卡号

begin
 st := fw_beep(icdev, 1);
 If st <> 0 Then begin
      List1.items.Add('调用 fw_beep()函数出错');
      Exit;
 End;
 List1.items.add('调用 fw_beep()函数成功');
 application.ProcessMessages;

case curCardType of

0:
  begin

 cardmode := 1;//MODE为1可反复寻卡, MODE为0卡必须拿开才可重新操作!
 address  := 4 ;
 sector   := 1 ;


 //st := fw_card(icdev,cardmode,tempint);   //不建议使用此函数，有时会返回带负号的卡号
 st:=fw_card_hex(icdev,cardmode,cardbuf);//返回16进制卡号

 If st <> 0 Then begin
      List1.items.add('调用 fw_card()函数出错');
      Exit;
End;
 List1.items.add('调用 fw_card()函数成功');
 //List1.items.add('序列号:'+inttostr(tempint));
 List1.Items.Add('序列号:'+cardbuf);

 st := fw_authentication(icdev, 0, sector);
 If st <> 0 Then begin
      List1.Items.Add('调用 fw_authentication()函数出错!');
      Exit;
 End;
 List1.Items.Add('调用 fw_authentication()函数成功!');


 sendbuf[0]:=ansichar($00);
 sendbuf[1]:=ansichar($11);
 sendbuf[2]:=ansichar($22);
 sendbuf[3]:=ansichar($33);
 sendbuf[4]:=ansichar($44);
 sendbuf[5]:=ansichar($55);
 sendbuf[6]:=ansichar($66);
 sendbuf[7]:=ansichar($77);
 sendbuf[8]:=ansichar($88);
 sendbuf[9]:=ansichar($99);
 sendbuf[10]:=ansichar($AA);
 sendbuf[11]:=ansichar($BB);
 sendbuf[12]:=ansichar($CC);
 sendbuf[13]:=ansichar($DD);
 sendbuf[14]:=ansichar($EE);
 sendbuf[15]:=ansichar($FF);

 st := fw_write(icdev, address, @sendbuf);   //the third block of each sector is key block
                                             //do not write this block when testing
 If st <> 0 Then begin
     List1.Items.add('调用 fw_write函数出错');
     Exit;
 End;
 List1.Items.add('调用 fw_write函数成功');
 //getmem(datadevStr,33);
 st := fw_read(icdev, address,readdata);
 If st <> 0 Then begin
     List1.Items.add('调用 fw_read函数出错');
     //freemem(datadevstr);
     Exit;
 End;
 List1.Items.add('调用 fw_read函数成功');
 str:=convertChararrToHexstr(readdata,16);
 List1.items.add('数据为:'+str);
// freemem(datadevstr);

st:=fw_initval(icdev,address,1000);
if st<>0 Then begin
  List1.Items.Add('调用fw_initval函数出错');
  Exit;
  End;
List1.Items.Add('调用fw_initval函数成功' );


 st:=fw_decrement(icdev,address,10);
 st:=fw_transfer(icdev,address);  //this function should be called after decrement,otherwise the changing
                                  //will be invalided
 if st<>0 Then begin
    List1.Items.Add('调用fw_decrement函数出错');
    List1.Items.Add('调用fw_transfer函数出错');
    Exit;
 End;

 List1.Items.Add('调用fw_decrement函数成功');
 List1.Items.Add('调用fw_transfer函数成功');

 st:=fw_readval(icdev,address,@value);
if st<>0 then begin
  List1.Items.Add('调用fw_readval函数出错') ;
  Exit;
  End;

  List1.Items.Add('调用fw_readval函数成功');
  List1.Items.Add('该块的值为：'+inttostr(value)) ;

 st := fw_halt(icdev);
 If st <> 0 Then begin
      List1.Items.add('调用 fw_halt()函数出错');
      Exit;
 End;
 List1.Items.add('调用 fw_halt()函数成功');
 List1.Items.add('读写设备测试通过!');
 End;   //case 0

1:
  begin

 cardmode := 1;//MODE为1可反复寻卡, MODE为0卡必须拿开才可重新操作!
 address  := 128+(34-32)*16 ;
 sector   := 34 ;


// st := fw_card(icdev,cardmode,tempint); //不建议使用此函数，有时会返回带负号的卡号
  st:=fw_card_hex(icdev,cardmode,cardbuf);//返回16进制卡号
 If st <> 0 Then begin
      List1.items.add('调用 fw_card()函数出错');
      Exit;
End;


 List1.items.add('调用 fw_card()函数成功');
// List1.items.add('序列号:'+inttostr(tempint));
 List1.items.add('序列号:'+cardbuf);

 st := fw_authentication(icdev, 0, sector);
 If st <> 0 Then begin
      List1.Items.Add('调用 fw_authentication()函数出错!');
      Exit;
 End;
 List1.Items.Add('调用 fw_authentication()函数成功!');


 sendbuf[0]:=ansichar($00);
 sendbuf[1]:=ansichar($11);
 sendbuf[2]:=ansichar($22);
 sendbuf[3]:=ansichar($33);
 sendbuf[4]:=ansichar($44);
 sendbuf[5]:=ansichar($55);
 sendbuf[6]:=ansichar($66);
 sendbuf[7]:=ansichar($77);
 sendbuf[8]:=ansichar($88);
 sendbuf[9]:=ansichar($99);
 sendbuf[10]:=ansichar($AA);
 sendbuf[11]:=ansichar($BB);
 sendbuf[12]:=ansichar($CC);
 sendbuf[13]:=ansichar($DD);
 sendbuf[14]:=ansichar($EE);
 sendbuf[15]:=ansichar($FF);

 st := fw_write(icdev, address, @sendbuf);
 If st <> 0 Then begin
     List1.Items.add('调用 fw_write函数出错');
     Exit;
 End;
 List1.Items.add('调用 fw_write函数成功');
 //getmem(datadevStr,33);
 st := fw_read(icdev, address,readdata);
 If st <> 0 Then begin
     List1.Items.add('调用 fw_read函数出错');
     //freemem(datadevstr);
     Exit;
 End;
 List1.Items.add('调用 fw_read函数成功');
 str:=convertChararrToHexstr(readdata,16);
 List1.items.add('数据为:'+str);
// freemem(datadevstr);

st:=fw_initval(icdev,address,1000);
if st<>0 Then begin
  List1.Items.Add('调用fw_initval函数出错');
  Exit;
  End;
List1.Items.Add('调用fw_initval函数成功' );


 st:=fw_decrement(icdev,address,10);
 st:=fw_transfer(icdev,address);
 if st<>0 Then begin
    List1.Items.Add('调用fw_decrement函数出错');
    List1.Items.Add('调用fw_transfer函数出错');
    Exit;
 End;

 List1.Items.Add('调用fw_decrement函数成功');
 List1.Items.Add('调用fw_transfer函数成功');

 st:=fw_readval(icdev,address,@value);
if st<>0 then begin
  List1.Items.Add('调用fw_readval函数出错') ;
  Exit;
  End;

  List1.Items.Add('调用fw_readval函数成功');
  List1.Items.Add('该块的值为：'+inttostr(value)) ;


 st := fw_halt(icdev);
 If st <> 0 Then begin
      List1.Items.add('调用 fw_halt()函数出错');
      Exit;
 End;
 List1.Items.add('调用 fw_halt()函数成功');
 List1.Items.add('读写设备测试通过!');
 End;   //case 1
 2:
 begin
 //寻卡
 st:=fw_request_ultralt(icdev,1);
 if st<>0 then begin
   List1.Items.Add('调用fw_request_ultralt 函数出错');
   Exit;
 End;
   List1.Items.Add('调用fw_request_ultralt函数成功');

 st:=fw_anticall_ultralt(icdev, tempint);
 if st<>0 then begin
   List1.Items.Add('调用fw_anticall_ultralt函数出错');
   Exit;
 End;

 List1.Items.Add('调用fw_anticall_ultralt函数成功');
 List1.items.add('序列号:'+inttostr(tempint));

 st:=fw_select_ultralt(icdev,tempint);
 if st<>0 then begin
   List1.Items.Add('调用fw_select_ultralt函数出错');
   Exit;
 End;
 List1.Items.Add('调用fw_select_ultralt函数成功');


 //写卡
 sendbuf[0]:=ansichar($11);
 sendbuf[1]:=ansichar($22);
 sendbuf[2]:=ansichar($33);
 sendbuf[3]:=ansichar($44);

 //读写卡之前先读一下第0块
 st:=fw_read_ultralt(icdev,0,readdata);//读第0块
 if st<>0 then begin
    List1.Items.Add('Read Page 0 出错');
    Exit;
 End;

 st:=fw_write_ultralt(icdev,4, @sendbuf); //写第4块
 if st<>0 then begin
   List1.Items.Add('调用函数fw_write_ultralt出错');
   Exit;
 End;
 List1.Items.Add('调用函数fw_write_ultralt成功');

 //读卡
 st:=fw_read_ultralt(icdev,4,readdata);     //读第4块
 if st<>0 then begin
   List1.Items.Add('调用函数fw_read_ultralt出错');
   Exit;
 End;
 List1.Items.Add('调用函数fw_read_ultralt成功');
 str:=convertChararrToHexstr(readdata,8);
 List1.items.add('数据为:'+str);

 End;
 else
 End;
 

end;

procedure Tfrm_demo_rf.Button5Click(Sender: TObject);

var
keyarr:Array [0..6] of ansichar;
sendbuf:Array[0..16]of ansichar;


begin
//下面例子将2扇区的A套密码从ffffffffffff
//改为112233445566,然后再改回ffffffffffff
//该该密码程序针对的卡型是MIFARE ONE
 cardmode := 1 ;
 sector   := 1 ;

 keyarr[0]:=ansichar($ff);
 keyarr[1]:=ansichar($ff);
 keyarr[2]:=ansichar($ff);
 keyarr[3]:=ansichar($ff);
 keyarr[4]:=ansichar($ff);
 keyarr[5]:=ansichar($ff);

 st := fw_load_key(icdev, 0, 1,@keyarr);
 If st <> 0 Then begin
      List1.items.add('调用 fw_Load_key()函数出错!');
      exit;
 end;
 List1.items.add('调用 fw_Load_key()函数成功!');

 st := fw_card(icdev,cardmode,tempint);
 If st <> 0 Then begin
      List1.items.add('调用 fw_card()函数出错');

      Exit;
 End;
 List1.items.add('调用 fw_card()函数成功');

 st := fw_authentication(icdev, 0, sector);
 If st <> 0 Then begin
      List1.Items.Add('调用 fw_authentication()函数出错!');
      Exit;
 End;
 List1.Items.Add('调用 fw_authentication()函数成功!');

//现在把A密码改为112233445566,B密码写为FFFFFFFFFFFF
 sendbuf[0]:=ansichar($11);
 sendbuf[1]:=ansichar($22);
 sendbuf[2]:=ansichar($33);
 sendbuf[3]:=ansichar($44);
 sendbuf[4]:=ansichar($55);
 sendbuf[5]:=ansichar($66);
 sendbuf[6]:=ansichar($FF);
 sendbuf[7]:=ansichar($07);
 sendbuf[8]:=ansichar($80);
 sendbuf[9]:=ansichar($69);
 sendbuf[10]:=ansichar($FF);
 sendbuf[11]:=ansichar($FF);
 sendbuf[12]:=ansichar($FF);
 sendbuf[13]:=ansichar($FF);
 sendbuf[14]:=ansichar($FF);
 sendbuf[15]:=ansichar($FF);

 // st := fw_write(icdev,1*4+3,@sendbuf);
 If st <> 0 Then begin
      List1.Items.Add('调用 fw_write()函数出错!');
      Exit;
 End;
    List1.Items.Add('A密码改为了112233445566');
 //上面两个写函数完成后,密码改为了112233445566


 //现在重新装入密码,才能正确操作
 keyarr[0]:=ansichar($11);
 keyarr[1]:=ansichar($22);
 keyarr[2]:=ansichar($33);
 keyarr[3]:=ansichar($44);
 keyarr[4]:=ansichar($55);
 keyarr[5]:=ansichar($66);

 st := fw_load_key(icdev, 0, 1,@keyarr);
 If st <> 0 Then begin
      List1.items.add('调用 fw_Load_key()函数出错!');
      exit;
 end;
 List1.items.add('调用 fw_Load_key()函数成功!');

  st := fw_card(icdev,cardmode,tempint);
 If st <> 0 Then begin
      List1.items.add('调用 fw_card()函数出错');

      Exit;
 End;
 List1.items.add('调用 fw_card()函数成功');

 st := fw_authentication(icdev, 0, sector);
 If st <> 0 Then begin
      List1.Items.Add('调用 fw_authentication()函数出错!');
      Exit;
 End;
 List1.Items.Add('调用 fw_authentication()函数成功!');

//现在开始把A密码改回ffffffffffff, b密码改回ffffffffffff
 sendbuf[0]:=ansichar($FF);
 sendbuf[1]:=ansichar($FF);
 sendbuf[2]:=ansichar($FF);
 sendbuf[3]:=ansichar($FF);
 sendbuf[4]:=ansichar($FF);
 sendbuf[5]:=ansichar($FF);
 sendbuf[6]:=ansichar($FF);
 sendbuf[7]:=ansichar($07);
 sendbuf[8]:=ansichar($80);
 sendbuf[9]:=ansichar($69);
 sendbuf[10]:=ansichar($FF);
 sendbuf[11]:=ansichar($FF);
 sendbuf[12]:=ansichar($FF);
 sendbuf[13]:=ansichar($FF);
 sendbuf[14]:=ansichar($FF);
 sendbuf[15]:=ansichar($FF);
 // st := fw_write(icdev,1*4+3,@sendbuf);
 If st <> 0 Then begin
      List1.Items.Add('调用 fw_write()函数出错!');
      Exit;
 End;

 //现在把密码改回了  ffffffffffff
 //  List1.Items.Add('密码改回了ffffffffffff');
//   List1.Items.Add('修改密码操作完成!');
end;

procedure Tfrm_demo_rf.Button4Click(Sender: TObject);
begin
quit();
close
end;


procedure Tfrm_demo_rf.FormCreate(Sender: TObject);
begin
ComboBox1.ItemIndex:=0 ;

end;

procedure Tfrm_demo_rf.ComboBox1Change(Sender: TObject);
begin
curCardType:=ComboBox1.ItemIndex;
if curCardType=2 Then
Button5.Enabled:=false
else
Button5.Enabled:=true;


end;

end.
